<%@page import="java.io.File"%>
<%@page import="BusinessLogic.Result.ResultType"%>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.LinkedList" %>
<%@ page import="BusinessLogic.Result" %>
<%@ page import="BusinessLogic.Session" %>
<%@ page import="BusinessLogic.CoursesContainer" %>
<%@ page import="BusinessLogic.Course" %>
<%@page import="BusinessLogic.User"%>
<%@ page import="java.util.*" %>
<jsp:useBean id="requestHandler" class="RequestHandlerPackage.RequestHandler" scope="application"/>
<%
String uName = request.getUserPrincipal().getName();
Map<String, String[]> params = new HashMap<String, String[]>();
Enumeration<String> map = request.getParameterNames();
while(map.hasMoreElements())
{
	String par = (String) map.nextElement();
	params.put(par, request.getParameterValues(par));
}
params.put("action",new String[]{"searchCourses"});
Result res = requestHandler.handle(params);
if (res.getResultType().equals(ResultType.FAILURE))
{
	%>
	<p><%=res.getFailureMessage() %></p>
	<%
	return;
}
CoursesContainer cc = null;
List<Course> courses = null;
if (res.getResultType().equals(ResultType.FAILURE)){
  	String adminUrl = request.getContextPath()+"/";	
	%>
	<script type="text/javascript">
	alert("The DB encountered a problem, please try again.");
	</script>
	<meta http-equiv="Refresh" content="5; url=<%=adminUrl %>"/>
	<% 
}else{
  cc = (CoursesContainer)res.getSuccessReturnValue();
  courses= cc.getCourses();
}
//check admin
params.clear();
params.put("action",new String[]{"getUser"});
params.put("USER_NAME",new String[]{uName});
params.put("me",new String[]{uName});
res = requestHandler.handle(params);
boolean admin = false;
if (res.getResultType().equals(ResultType.SUCCESS))
{
	if (((User)(res.getSuccessReturnValue())).getRole().equals("admin")){
	  	admin = true;
	}
}
List<Course> available = new LinkedList<Course>();
List<Course> registered = new LinkedList<Course>();
if (!admin)
{
	params.clear();
	params.put("action",new String[]{"availableCourses"});
	params.put("USER_NAME",new String[]{uName});
	res = requestHandler.handle(params);
	if (res.getResultType().equals(ResultType.SUCCESS))
	{
		available = (List<Course>)res.getSuccessReturnValue();
	}
	params.clear();
	params.put("action",new String[]{"getTimetable"});
	params.put("USER",new String[]{uName});
	res = requestHandler.handle(params);
	if (res.getResultType().equals(ResultType.SUCCESS))
	{
		CoursesContainer reg = (CoursesContainer)res.getSuccessReturnValue();
		registered = reg.getCourses();
	}
}
%>
						<input type="checkbox" name="available" value="available" onclick="onlyAvailable()"/>Show Only registration available courses
						<% for (Course c : courses){
							String courseName = c.getName();
							String divCourse = "\"div"+c.getNameNoSpaces()+"\"";
							String clas = "";
							if (!available.contains(c))
								clas = "class=\"av\"";
							%>
							<div id=<%= divCourse+" "+clas%> >
								<table>
								<tr>
								<td>
								<h4 class="course"><a  class="course" onclick="showCourseDetails('<%=courseName %>')"><%=c.getName()%></a></h4>
								</td>
								<%String me = request.getUserPrincipal().getName(); 
								  if (available.contains(c))
								  {
								  %>
								  <td>
								  <br/>
								  	<button type="button" onclick="registerCourse('<%=me%>','<%=courseName%>')">Register!</button>
								  </td>
								  <%} 
								  if (registered.contains(c))
								  {
								  %>
								  <td>
								  <br/>
								  	<button type="button" onclick="unregisterCourse('<%=me%>','<%=courseName%>')">Unregister!</button>
								  </td>
								  <%}%>
								  </tr>
								</table>
								<div class="adminkey" id="<%=c.getNameNoSpaces()%>">
									<p><span class="red">Description:</span> <%=c.getDescription()%></p>
									<p><span class="red">Capacity:</span> <%=c.getCapacity()%></p>
									<p><span class="red">Group:</span> <%=c.getGroup()%></p>
									<p><span class="red">Credit points:</span> <%=c.getPoints()%></p>
									<p>Sessions:</p>
									<% List<Session> sessions = cc.getSessionsOfCourse(courseName);
									for (Session s : sessions){%>
									  <p><span class="red">day:</span> <%=s.getDay()%></p>
									  <p><span class="red">Start time:</span> <%=s.getStart_time()+" :00" %></p>
									  <p><span class="red">End time:</span> <%=s.getEnd_time()+" :00" %></p>
									<%}%>
								</div>
							</div>
						<%}%>